// 班级列表
let classList = async (ctx, next) => {
  let classData = await ctx.db.querys(`select * from class`)
  ctx.body = { code: 1, msg: '用户列表数据获取成功', data: classData }
}


// 班级列表模糊搜索
let searchClassList = async (ctx, next) => {
  const { inpValue, school, course } = ctx.request.body;
  let classData = await ctx.db.querys(`select * from class`)

  if (!classData.length) {
    return ctx.body = { code: 0, msg: '暂无数据', data: [] }
  }

  if (inpValue !== '') {
    let arr1 = await ctx.db.querys(`select * from class`)
    arr1 = arr1.filter(item => item.className.indexOf(inpValue) !== -1)
    let arr2 = await ctx.db.querys(`select * from user`)
    arr2 = arr2.filter(item => item.name.indexOf(inpValue) !== -1)
    if (arr2.length) {
      let arr3 = await ctx.db.querys(`select * from class where teacherid='${arr2[0].userid}'`)
      classData = arr3
    } else {
      classData = arr1
    }
  }


  if (school !== '全部') {
    classData = classData.filter(item => item.school === school)
  }

  if (course !== '全部') {
    classData = classData.filter(item => item.course === course)
  }

  ctx.body = { code: 1, msg: '数据请求成功', data: classData }

}


// 开课
let startClass = async (ctx, next) => {

  let isOpr = await ctx.isJudge(ctx, 'S');
  if (!isOpr) {
    ctx.body = { code: 0, msg: '您无权操作' }
    return
  }
  // 当前登录用户如果存在产出的权限 则可执行冻结
  const { classid, startTime, teacherid, course } = ctx.request.body
  let dbReturn = await ctx.db.querys(`update class set startTime='${startTime}',teacherid="${teacherid}",status="no",course="${course}" where classid='${classid}'`)
  if (!dbReturn.affectedRows) {
    ctx.body = { code: 0, msg: '操作失败,请重新再来' }
    return
  }
  ctx.body = { code: 1, msg: '成功设置开课时间' }
}



// 结课
let endClass = async (ctx, next) => {
  let isOpr = await ctx.isJudge(ctx, 'O');
  if (!isOpr) {
    ctx.body = { code: 0, msg: '您无权操作' }
    return
  }

  const { classid } = ctx.request.body;
  let startTimeList = await ctx.db.querys(`select startTime,course,teacherid,coachid,school,classType,status from class where classid='${classid}'`)
  if (!startTimeList.length) {
    return ctx.body = { code: 0, msg: '未找到该数据' }
  }

  let status = startTimeList[0].status
  if (status === 'ok') {
    ctx.body = { code: 0, msg: '已结课,请勿重复点击' }
    return
  }




  await ctx.db.querys(`update class set status='ok' where classid='${classid}'`)

  let endTime = new Date().getTime() + ''

  let startTime = startTimeList[0].startTime;
  let course = startTimeList[0].course;
  let teacherid = startTimeList[0].teacherid;
  let coachid = startTimeList[0].coachid;
  let school = startTimeList[0].school;
  let classType = startTimeList[0].classType;

  let results = await ctx.db.querys(`insert into log (endTime,classid,startTime,course,teacherid,coachid,school,classType) values ('${endTime}','${classid}','${startTime}','${course}','${teacherid}','${coachid}','${school}',${classType})`)


  if (results.affectedRows === 1) {
    ctx.body = { code: 1, msg: '添加成功' }
    return
  }
  ctx.body = { code: 0, msg: '添加失败' }
}





// 添加班级
let addClass = async (ctx, next) => {
  let isOpr = await ctx.isJudge(ctx, 'A');

  // 判断当前用户是否可以操作
  if (!isOpr) {
    ctx.body = { code: -2, msg: '您无权操作' }
    return
  }

  // 如果权限够  就可以添加
  const { className, school, teacherid, coachid, course, classType } = ctx.request.body;

  let classList = await ctx.db.querys(`select * from class where school='${school}'`)
  let index = classList.findIndex(item => item.className === className)
  if (index !== -1) {
    ctx.body = { code: 0, msg: '已存在该班级名称,请更换班级名称后重试' }
    return
  }

  let classid = Math.floor(Math.random() * 99999999)
  await ctx.db.querys(`insert into setlist (classid) values ('${classid}')`)
  let results = await ctx.db.querys(`insert into class (className,school,teacherid,coachid,course,classType,startTime,classid) values ('${className}','${school}','${teacherid}','${coachid}','${course}',${classType * 1},'0','${classid}')`)

  if (results.affectedRows === 1) {
    ctx.body = { code: 1, msg: '添加成功' }
    return
  }
  ctx.body = { code: -1, msg: '添加失败' }
}



export default {
  classList, searchClassList, startClass, endClass, addClass
}